package com.qire.manhua.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.CustomEvent;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.utils.NoticeEventBus;
import com.qire.manhua.R;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.BaseFragment;
import com.qire.manhua.controller.RecommendController;
import com.qire.manhua.databinding.FragmentRecommendBinding;
import com.qire.manhua.dialog.MemberOverduePromptDialog;
import com.qire.manhua.dialog.TeensEnablePromptDialog;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.RecommendViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 首页(Home) -> 推荐页
 */
@RouteMapping(path = Navigation.Fragment.RecommendFragment)
public class RecommendFragment extends BaseFragment<FragmentRecommendBinding> {

    @Autowired
    int pageNo = PageTabIndex.TAB_INDEX_RECOMMEND_HOT;

    @Autowired
    RecommendViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    RecommendController controller;

    @Override
    public int layoutRid() {
        return R.layout.fragment_recommend;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        registerObserver();
        FragmentRecommendBinding viewDataBinder = viewDataBinder(inflater, container);

        if(ObjectUtil.nonNull(viewModel)) {
            viewDataBinder.setRecommendViewModel(viewModel);
            viewModel.onAddition(this);
            viewModel.refreshUserRelatedUI();
        }

        if(ObjectUtil.nonNull(controller)) {
            controller.onAddition(viewModel);
        }

        return viewDataBinder.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        MemberOverduePromptDialog.showIfNeedTips(getContext());
        // NOTE: 跳转页面后重置选项卡索引，避免重复进入，仅在第一次或其他页面跳转时注入索引才触发跳转子页
        if(pageNo != PageTabIndex.TAB_INDEX_INVALID) {
            viewDataBinder().recommendViewPager.postCurrentItem(pageNo);
            pageNo = PageTabIndex.TAB_INDEX_INVALID;
        }
    }

    @Override
    protected void onReEnter() {
        super.onReEnter();
        TeensEnablePromptDialog.buildAndShowIfNeeded(getContext());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterObserver();
    }

    protected void registerObserver() {
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.USER_UPDATE_REFRESH, loginSuccessHandle);
    }

    protected void unregisterObserver() {
        NoticeEventBus.NOTICE.unSubscribeEvent(loginSuccessHandle);
    }

    private NoticeEventBus.EventSubscriber loginSuccessHandle = noticeEvent -> {
        viewModel.refreshUserRelatedUI();
        return false;
    };


}
